package com.coai.warehouse.controller

import com.coai.common.dto.ApiResponse
import com.coai.warehouse.model.*
import com.coai.warehouse.service.*
import kotlinx.coroutines.runBlocking
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*
import java.time.LocalDateTime

@RestController
@RequestMapping("/api/warehouse")
class WarehouseController(
    private val warehouseService: WarehouseService
) {
    
    /**
     * AI智能选址推荐
     */
    @PostMapping("/locations/recommend")
    fun recommendLocation(
        @RequestBody request: LocationRecommendRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<WarehouseLocation> = runBlocking {
        val location = warehouseService.recommendLocation(
            warehouseId = request.warehouseId,
            productId = request.productId,
            quantity = request.quantity,
            requiredCapacity = request.requiredCapacity
        )
        ApiResponse.success(location, "推荐库位成功")
    }
    
    /**
     * AI优化拣货路径
     */
    @PostMapping("/outbound/{orderId}/optimize-path")
    fun optimizePickingPath(
        @PathVariable orderId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<PickingPathResult> = runBlocking {
        val result = warehouseService.optimizePickingPath(orderId)
        ApiResponse.success(result, "路径优化完成")
    }
    
    /**
     * AI质量检测
     */
    @PostMapping("/inbound/items/{itemId}/quality-check")
    fun performQualityCheck(
        @PathVariable itemId: String,
        @RequestBody imageData: ByteArray,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<QualityCheckResult> = runBlocking {
        val result = warehouseService.performAIQualityCheck(itemId, imageData)
        ApiResponse.success(result, "质检完成")
    }
    
    /**
     * 库存预警分析
     */
    @GetMapping("/{warehouseId}/inventory/alerts")
    fun getInventoryAlerts(
        @PathVariable warehouseId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<List<InventoryAlert>> = runBlocking {
        val alerts = warehouseService.analyzeInventoryAlerts(warehouseId)
        ApiResponse.success(alerts)
    }
    
    /**
     * 创建入库单
     */
    @PostMapping("/inbound")
    fun createInboundOrder(
        @RequestBody request: CreateInboundOrderRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<InboundOrder> {
        val order = warehouseService.createInboundOrder(
            warehouseId = request.warehouseId,
            supplierId = request.supplierId,
            type = request.type,
            items = request.items,
            plannedTime = request.plannedTime
        )
        return ApiResponse.success(order, "入库单创建成功")
    }
    
    /**
     * 创建出库单
     */
    @PostMapping("/outbound")
    fun createOutboundOrder(
        @RequestBody request: CreateOutboundOrderRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<OutboundOrder> {
        val order = warehouseService.createOutboundOrder(
            warehouseId = request.warehouseId,
            customerId = request.customerId,
            type = request.type,
            items = request.items,
            priority = request.priority,
            plannedTime = request.plannedTime
        )
        return ApiResponse.success(order, "出库单创建成功")
    }
}

// Request DTOs
data class LocationRecommendRequest(
    val warehouseId: String,
    val productId: String,
    val quantity: Int,
    val requiredCapacity: Double
)

data class CreateInboundOrderRequest(
    val warehouseId: String,
    val supplierId: String,
    val type: InboundType,
    val items: List<InboundItemRequest>,
    val plannedTime: LocalDateTime
)

data class CreateOutboundOrderRequest(
    val warehouseId: String,
    val customerId: String,
    val type: OutboundType,
    val items: List<OutboundItemRequest>,
    val priority: Priority,
    val plannedTime: LocalDateTime
)
